package cn.buk.tms.dto;

import cn.buk.common.util.DecimalUtil;
import cn.buk.tms.entity.misc.Customer;
import cn.buk.tms.entity.misc.PaymentMethod;
import cn.buk.tms.entity.misc.Supplier;
import cn.buk.tms.entity.order.train.TrainOrder;
import cn.buk.tms.entity.order.train.TrainOrderPassenger;
import com.fasterxml.jackson.annotation.JsonFormat;
import org.springframework.beans.BeanUtils;

import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *
 * @author yfdai
 * @date 2018-10-29
 */
public class TrainOrderDto {

    public static TrainOrderDto createByTrainOrder(final TrainOrder o) {
        TrainOrderDto dto = new TrainOrderDto();

        BeanUtils.copyProperties(o, dto);

        dto.setPassengers(new ArrayList<>());
        String psgNames = null;
        for(TrainOrderPassenger psg: o.getPassengers()) {

            TrainOrderPassengerDto psgDto = new TrainOrderPassengerDto();

            BeanUtils.copyProperties(psg, psgDto);

            dto.getPassengers().add(psgDto);

            if (psgNames == null) {
              psgNames = psg.getName();
            } else {
              psgNames += "," + psg.getName();
            }
        }

        if (psgNames != null) {
            dto.setPsgCount(dto.getPassengers().size());
            dto.setPassengerName(psgNames);
        }


        if (o.getCustomer() == null) {
            dto.setCustomerId(0);
            dto.setCustomerName("散客");
        } else {
            dto.setCustomerId(o.getCustomerId());
            dto.setCustomerName(o.getCustomer().getVipName());
            dto.setCustomerCode(o.getCustomer().getCustomerCode());
        }

        if (o.getSupplier() != null) {
            dto.setSupplierId(o.getSupplier().getId());
            dto.setSupplierName(o.getSupplier().getName());
        }

        if (o.getPaymentMethod() != null) {
            dto.setPaymentMethodId(o.getPaymentMethod().getId());
            dto.setPaymentMethodName(o.getPaymentMethod().getName());
        }

        return dto;
    }

    public static TrainOrder convertToTrainOrder(TrainOrderDto dto) {
        TrainOrder o = new TrainOrder();
        BeanUtils.copyProperties(dto, o);

        o.setPassengers(new ArrayList<>());
        String psgNames = null;
        for(TrainOrderPassengerDto psgDto: dto.getPassengers()) {

            TrainOrderPassenger psg = new TrainOrderPassenger();
            psg.setOrderInfo(o);
            psg.setEnterpriseId(o.getEnterpriseId());

            BeanUtils.copyProperties(psgDto, psg);



            o.getPassengers().add(psg);

            if (psgNames == null) {
              psgNames = psgDto.getName();
            } else {
              psgNames += "," + psgDto.getName();
            }
        }

        o.setPsgCount(o.getPassengers().size());
        o.setPassengerName(psgNames);


        o.setCustomer(null);
        if (dto.getCustomerId() > 0) {
            Customer c = new Customer();
            c.setId(dto.getCustomerId());

            o.setCustomer(c);
            //企业客户
            o.setCustomerType(1);
        } else {
            // 散客
            o.setCustomerType(0);
        }

        if (dto.getSupplierId() > 0) {
            Supplier supplier = new Supplier();
            supplier.setId(dto.getSupplierId());

            o.setSupplier(supplier);
        }

        if (dto.getPaymentMethodId() > 0) {
            PaymentMethod pm = new PaymentMethod();
            pm.setId(dto.getPaymentMethodId());

            o.setPaymentMethod(pm);
        }

        return o;
    }

    private int id;

    private int enterpriseId;

    private String orderNo;

    /**
     * 外部订单号
     */
    @Size(max = 20)
    private String externalOrderNo;

    private Integer customerId;

    private String customerName;

    private String customerCode;

    private String costCenter;

    private String projectName;

    /**
     * 账单号
     */
    private String billNum;

    /**
     * 销售日期
     */
    @JsonFormat(pattern = "yyyy-MM-dd",timezone="GMT+8")
    private Date saleDate;

    /**
     * 付款方式
     */
    private int payType = 8;

    private int payStatus;



    //姓名 车次 出发地 目的地 日期 座位等级 价格 服务费

    /**
     * 姓名
     * 暂时保留
     */
    @Deprecated
    @NotNull
    @Size(min = 1)
    private String passengerName;

    /**
     * 人数
     */
    private int psgCount;

    /**
     * 乘客信息
     */
    @NotNull
    @Size(min = 1, max = 1000)
    private List<TrainOrderPassengerDto> passengers = new ArrayList<>();

    /**
     * 车次
     */
    @NotNull
    @Size(min = 1)
    private String trainNo;

    /**
     * 出发地
     */
    @NotNull
    @Size(min = 1)
    private String dcity;

    /**
     * 目的地
     */
    @NotNull
    @Size(min = 1)
    private String acity;

    /**
     * 日期
     */
    @NotNull
    @JsonFormat(pattern = "yyyy-MM-dd",timezone="GMT+8")
    private Date ddate;

    private String dtime;

    private String atime;

    /**
     * 座位等级
     */
    @NotNull
    @Size(min = 1)
    private String seatClass;

    /**
     * 销售价/人
     */
    private double price;

    /**
     * 成本价/人
     */
    private double cost;

    /**
     * 服务费
     */
    private double serviceCharge;

    private double totalAmount;

    private double totalCost;

    private double profit;

    private int status;

    /**
     * 是否已付款
     */
    private String bePaid;

    private int supplierId;

    private String supplierName;

    private int paymentMethodId;

    private String paymentMethodName;

    private String remark;

    /**
     * 保存数据库的时间：系统自动生成
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
    private Date createTime;


    /**
     * 创建此账单的用户名
     */
    private String operator;

    /**
     * 出票员
     */
    private String ticketer;

    /**
     * 收款员（如果是月结订单，该值为空)
     */
    private String cashier;

    /**
     * 计算价格
     */
    public void calc() {
        this.totalAmount = this.psgCount * this.price + this.serviceCharge;
        this.totalCost = this.psgCount * this.cost;
        this.profit = this.totalAmount - this.totalCost;
    }

    public void processPassengerInfo() {

        String psgNames = null;
        for(TrainOrderPassengerDto psg: this.getPassengers()) {
            if (psgNames == null) {
              psgNames = psg.getName();
            } else {
              psgNames += "," + psg.getName();
            }
        }

        if (psgNames != null) {
            if (psgNames.length() > 50) {
                psgNames = psgNames.substring(0, 40);
            }

            this.passengerName = psgNames;
            this.psgCount = this.getPassengers().size();
        }
    }


    public double getTotal() {
        return this.price + this.serviceCharge;
    }


    public int getPaymentId() {
        return paymentMethodId;
    }

    public void setPaymentId(int paymentMethodId) {
        this.paymentMethodId = paymentMethodId;
    }



    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getEnterpriseId() {
        return enterpriseId;
    }

    public void setEnterpriseId(int enterpriseId) {
        this.enterpriseId = enterpriseId;
    }

    public String getBillNum() {
        return billNum;
    }

    public void setBillNum(String billNum) {
        this.billNum = billNum;
    }

    public Date getSaleDate() {
        return saleDate;
    }

    public void setSaleDate(Date saleDate) {
        this.saleDate = saleDate;
    }



    public String getPassengerName() {
        return passengerName;
    }

    public void setPassengerName(String passengerName) {
        this.passengerName = passengerName;
    }

    public String getTrainNo() {
        return trainNo;
    }

    public void setTrainNo(String trainNo) {
        this.trainNo = trainNo;
    }

    public String getDcity() {
        return dcity;
    }

    public void setDcity(String dcity) {
        this.dcity = dcity;
    }

    public String getAcity() {
        return acity;
    }

    public void setAcity(String acity) {
        this.acity = acity;
    }

    public Date getDdate() {
        return ddate;
    }

    public void setDdate(Date ddate) {
        this.ddate = ddate;
    }

    public String getSeatClass() {
        return seatClass;
    }

    public void setSeatClass(String seatClass) {
        this.seatClass = seatClass;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public double getServiceCharge() {
        return serviceCharge;
    }

    public void setServiceCharge(double serviceCharge) {
        this.serviceCharge = serviceCharge;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public String getBePaid() {
        return bePaid;
    }

    public void setBePaid(String bePaid) {
        this.bePaid = bePaid;
    }

    public int getCustomerId() {
        return customerId == null ? 0 : customerId;
    }

    public void setCustomerId(Integer customerId) {
        this.customerId = customerId;
    }

    public double getCost() {
        return cost;
    }

    public void setCost(double cost) {
        this.cost = cost;
    }

    public double getTotalAmount() {
        return totalAmount;
    }

    public void setTotalAmount(double totalAmount) {
        this.totalAmount = totalAmount;
    }

    public double getTotalCost() {
        return DecimalUtil.keepScale2(totalCost);
    }

    public void setTotalCost(double totalCost) {
        this.totalCost = totalCost;
    }

    public double getProfit() {
        return DecimalUtil.keepScale2(profit);
    }

    public void setProfit(double profit) {
        this.profit = profit;
    }

    public int getPsgCount() {
        return psgCount;
    }

    public void setPsgCount(int psgCount) {
        this.psgCount = psgCount;
    }

    public int getSupplierId() {
        return supplierId;
    }

    public void setSupplierId(int supplierId) {
        this.supplierId = supplierId;
    }

    public String getSupplierName() {
        return supplierName;
    }

    public void setSupplierName(String supplierName) {
        this.supplierName = supplierName;
    }

    public int getPaymentMethodId() {
        return paymentMethodId;
    }

    public void setPaymentMethodId(int paymentMethodId) {
        this.paymentMethodId = paymentMethodId;
    }

    public String getPaymentMethodName() {
        return paymentMethodName;
    }

    public void setPaymentMethodName(String paymentMethodName) {
        this.paymentMethodName = paymentMethodName;
    }

    public String getOrderNo() {
        return orderNo;
    }

    public void setOrderNo(String orderNo) {
        this.orderNo = orderNo;
    }

    public String getCustomerName() {
        return customerName;
    }

    public void setCustomerName(String customerName) {
        this.customerName = customerName;
    }

    public String getCustomerCode() {
        return customerCode;
    }

    public void setCustomerCode(String customerCode) {
        this.customerCode = customerCode;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public List<TrainOrderPassengerDto> getPassengers() {
        return passengers;
    }

    public void setPassengers(List<TrainOrderPassengerDto> passengers) {
        this.passengers = passengers;
    }

    public String getExternalOrderNo() {
        return externalOrderNo;
    }

    public void setExternalOrderNo(String externalOrderNo) {
        this.externalOrderNo = externalOrderNo;
    }

    public String getDtime() {
        return dtime;
    }

    public void setDtime(String dtime) {
        this.dtime = dtime;
    }

    public String getAtime() {
        return atime;
    }

    public void setAtime(String atime) {
        this.atime = atime;
    }

    public String getCostCenter() {
        return costCenter;
    }

    public void setCostCenter(String costCenter) {
        this.costCenter = costCenter;
    }

    public String getProjectName() {
        return projectName;
    }

    public void setProjectName(String projectName) {
        this.projectName = projectName;
    }

    public int getPayStatus() {
        return payStatus;
    }

    public void setPayStatus(int payStatus) {
        this.payStatus = payStatus;
    }

    public int getPayType() {
        return payType;
    }

    public void setPayType(int payType) {
        this.payType = payType;
    }

    public String getOperator() {
        return operator;
    }

    public void setOperator(String operator) {
        this.operator = operator;
    }

    public String getTicketer() {
        return ticketer;
    }

    public void setTicketer(String ticketer) {
        this.ticketer = ticketer;
    }

    public String getCashier() {
        return cashier;
    }

    public void setCashier(String cashier) {
        this.cashier = cashier;
    }
}
